package com.example.blog.modules.log.service.impl;

import com.example.blog.modules.log.dto.OperationLogDTO;
import com.example.blog.modules.log.entity.OperationLog;
import com.example.blog.modules.log.repository.OperationLogRepository;
import com.example.blog.modules.log.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
@RequiredArgsConstructor
public class OperationLogServiceImpl implements OperationLogService {
    
    private final OperationLogRepository operationLogRepository;
    
    @Override
    @Transactional
    public void save(OperationLogDTO logDTO) {
        OperationLog log = convertToEntity(logDTO);
        operationLogRepository.save(log);
    }
    
    @Override
    public Page<OperationLogDTO> findByUsername(String username, Pageable pageable) {
        return operationLogRepository.findByUsername(username, pageable)
                .map(this::convertToDTO);
    }
    
    @Override
    public Page<OperationLogDTO> findByOperation(String operation, Pageable pageable) {
        return operationLogRepository.findByOperationContaining(operation, pageable)
                .map(this::convertToDTO);
    }
    
    @Override
    public Page<OperationLogDTO> findAll(Pageable pageable) {
        return operationLogRepository.findAll(pageable)
                .map(this::convertToDTO);
    }
    
    @Override
    @Transactional
    public void deleteById(Long id) {
        operationLogRepository.deleteById(id);
    }
    
    @Override
    @Transactional
    public void deleteByIds(Long[] ids) {
        operationLogRepository.deleteAllById(Arrays.asList(ids));
    }
    
    private OperationLog convertToEntity(OperationLogDTO dto) {
        OperationLog entity = new OperationLog();
        entity.setUsername(dto.getUsername());
        entity.setOperation(dto.getOperation());
        entity.setMethod(dto.getMethod());
        entity.setParams(dto.getParams());
        entity.setIp(dto.getIp());
        entity.setUserAgent(dto.getUserAgent());
        entity.setRequestUrl(dto.getRequestUrl());
        entity.setExecutionTime(dto.getExecutionTime());
        entity.setDescription(dto.getDescription());
        entity.setSuccess(dto.isSuccess());
        entity.setErrorMessage(dto.getErrorMessage());
        return entity;
    }
    
    private OperationLogDTO convertToDTO(OperationLog entity) {
        OperationLogDTO dto = new OperationLogDTO();
        dto.setId(entity.getId());
        dto.setUsername(entity.getUsername());
        dto.setOperation(entity.getOperation());
        dto.setMethod(entity.getMethod());
        dto.setParams(entity.getParams());
        dto.setIp(entity.getIp());
        dto.setUserAgent(entity.getUserAgent());
        dto.setRequestUrl(entity.getRequestUrl());
        dto.setExecutionTime(entity.getExecutionTime());
        dto.setDescription(entity.getDescription());
        dto.setSuccess(entity.isSuccess());
        dto.setErrorMessage(entity.getErrorMessage());
        dto.setCreatedAt(entity.getCreatedAt());
        return dto;
    }
} 